num_cells=size(ROIs,2);
x=aux_data(9,frame_times);
y=aux_data(12,frame_times);
nbins=100;
bins=[];
step=range([-4 4])/nbins;
xy=[];
cnt=1;

for t=1:length(x)
    xy(:,:,t)=[x(t) y(t)];
end

for i=1:nbins+1
    for j=1:nbins+1
        bins(:,:,cnt)=[4-((i-1)*step) 4-((j-1)*step)];
        cnt=cnt+1;
    end
end

binx=[];
biny=[];
dnt=1;

for f=1:nbins
    for g=1:nbins
        binx(:,:,dnt)=[bins(1,1,1+((nbins+1)*(f-1))) bins(1,1,(nbins+2)+((nbins+1)*(f-1)))];
        biny(:,:,dnt)=[bins(1,2,g) bins(1,2,g+1)];
        dnt=dnt+1;
    end
end

acti_mat=[];
for lnd=1:num_cells
    cell_num=lnd;
    acti=ROIs(cell_num).activity;
    raw_acti=acti/median(acti);
    acti_mat(lnd,:)=raw_acti;
end

activity_mat=nan(num_cells,(nbins*nbins));
for k=1:(nbins*nbins)
    activity_mat(:,k)=mean(acti_mat(:,xy(1,1,:)<=binx(1,1,k)&xy(1,1,:)>binx(1,2,k)&xy(1,2,:)<=biny(1,1,k)&xy(1,2,:)>biny(1,2,k)),2);
end

act_2d=[];
for gnd=1:num_cells
    for ind=1:nbins
        act_2d(ind,:,gnd)=activity_mat(gnd,1+(nbins*(ind-1)):nbins+(nbins*(ind-1)));
    end
end

%act_2d(isnan(act_2d))=1;
for hnd=1:num_cells
    figure('color','black');
    axes('position',[0.02 0.1 0.86 0.85])
    imagesc(act_2d(:,:,hnd))
    colorbar
    title(strcat('Cell ',num2str(hnd)),'color','white')
    axes('position',[0.02 0.01 0.86 0.08])
    plot(acti_mat(hnd,:),'color','black')
end

% % % cnt=0;
% % % num_fig_mat=[];
% % % g=1;
% % % for n=1:num_cells
% % %     cnt=cnt+1;
% % %     num_fig_mat(n,g)=n;
% % %     if cnt==10
% % %         cnt=0;
% % %         g=g+1;
% % %     end
% % % end
% % % 
% % % num_fig=size(num_fig_mat,2);
% % % gnt=1;
% % % for z=1:num_fig
% % %     cells=unique(num_fig_mat(:,z));
% % %     cells(cells==0)=[];
% % %     figure('color','white')%,'Visible','off')
% % %     for m=1:length(cells)
% % %         axes('position',[0.02 0.9-(0.1*(m-1)) 0.95 0.1])
% % %         hold on
% % %         plot(acti_mat(gnt,:),'color','black')
% % %         axis off
% % %         text(size(acti_mat,2),0.9-(0.1*(m-1)),num2str(gnt),'FontWeight','bold','color','blue')
% % %         gnt=gnt+1;
% % %     end
% % % end